﻿using Datav.Design.Common;
using Datav.Design.IService;
using XCode;
using Zdap.Common;
using Zdap.Common.Extensions;
using Zdap.XCode;
using Zdap.XCode.Service;

namespace Datav.Design.Service
{
    /// <summary>
    ///
    /// </summary>
    public class QuestionService : BaseService<Question, QuestionTagDto, QuestionTagDto>, IQuestionService
    {
        /// <summary>
        ///
        /// </summary>
        /// <param name="dto"></param>
        /// <param name="user"></param>
        /// <returns></returns>
        public override ValueTask<ResponseContext<long>> Save(QuestionTagDto dto, UserTicket user)
        {
            return base.Save(dto, user);
        }

        /// <summary>
        ///
        /// </summary>
        /// <param name="p"></param>
        /// <returns></returns>
        public override Task<PageResponseContext<QuestionTagDto>> Search(Pager p)
        {
            //SetPolicy(p.Params["ProjectId"].ToLong());
            return base.Search(p);
        }

        /// <summary>
        ///
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        protected override async ValueTask<ResponseContext<bool>> AddValidate(QuestionTagDto dto)
        {
            var result = new ResponseContext<bool>();
            if (dto.ProjectId == 0)
            {
                return await Task.FromResult(result.SetError("新增内容案例Id不能为0！"));
            }
            if (dto.QuestionCategoryId == 0)
            {
                return await Task.FromResult(result.SetError("新增内容案例Id不能为0！"));
            }

            result = await Verify(dto, result);
            if (!result.IsSuccess)
            {
                return result;
            }

            return await base.AddValidate(dto);
        }

        private async ValueTask<ResponseContext<bool>> Verify(QuestionTagDto dto, ResponseContext<bool> result)
        {
            var project = await Project.FindCountAsync(Project._.Id == dto.ProjectId & Project._.IsDelete == 0);
            if (project == 0)
            {
                return await Task.FromResult(result.SetError("案例不存在！"));
            }
            var category = await QuestionCategory.FindCountAsync(QuestionCategory._.Id == dto.QuestionCategoryId & QuestionCategory._.IsDelete == 0);
            if (category == 0)
            {
                return await Task.FromResult(result.SetError("业务分类不存在！"));
            }
            return result;
        }

        /// <summary>
        ///
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        protected override async ValueTask<ResponseContext<bool>> UpdateValidate(QuestionTagDto dto)
        {
            var result = new ResponseContext<bool>();
            if (dto.ProjectId == 0)
            {
                return await Task.FromResult(result.SetError("编辑内容案例Id不能为0！"));
            }
            if (dto.QuestionCategoryId == 0)
            {
                return await Task.FromResult(result.SetError("编辑内容案例Id不能为0！"));
            }
            result = await Verify(dto, result);

            if (!result.IsSuccess)
            {
                return result;
            }
            return await base.UpdateValidate(dto);
        }

        /// <summary>
        /// 移动题目
        /// </summary>
        /// <returns></returns>
        public async ValueTask<ResponseContext<bool>> MoveQuestion(MoveQuestionDto model, UserTicket currentUser)
        {
            //SetPolicy(model.ProjectId);
            var result = new ResponseContext<bool>();
            if (model.Ids.IsNullOrEmpty())
                return await Task.FromResult(result);

            model.Ids.Foreach(e =>
            {
                CommonService.Update<Question>([Question.__.QuestionCategoryId, Question.__.UpdateTime], [model.QuestionCategoryId, DateTime.Now], [Question.__.Id], [e]);
            });
            result.SetSuccess(true);

            return await Task.FromResult(result);
        }

        /// <summary>
        ///
        /// </summary>
        /// <param name="model"></param>
        /// <param name="currentUser"></param>
        /// <returns></returns>
        public async ValueTask<ResponseContext<bool>> PublishingOnCancel(PublishingOnCancelDto model, UserTicket currentUser)
        {
            var result = new ResponseContext<bool>();
            var e = Question.FindById(model.QuestionId);

            e.Status = (model.IsPublishing ? 1 : 0);
            e.PublishTime = DateTime.Now;
            e.PublishBy = currentUser.Id;

            CommonService.SetUpdateProperty(e, currentUser);
            e.Update();
            result.SetSuccess();
            return await Task.FromResult(result);
        }

        /// <summary>
        /// 批量发布
        /// </summary>
        /// <param name="ids"></param>
        /// <param name="user"></param>
        /// <returns></returns>
        public async ValueTask<ResponseContext<bool>> Publish(List<long> ids, UserTicket user)
        {
            var result = new ResponseContext<bool>();
            if (ids == null || !ids.Any())
            {
                return result;
            }
            var questions = await Question.FindAllAsync(Question._.Id.In(ids) & Question._.Status == QEnum.NotStart);
            if (questions.Any())
            {
                questions.ForEach(item =>
                {
                    item.Status = (int)QEnum.Started;
                    item.PublishTime = DateTime.Now;
                    item.PublishBy = user.Id;
                });

                questions.Save();
                result.SetSuccess();
            }
            return await Task.FromResult(result);
        }

        /// <summary>
        /// 批量取消发布
        /// </summary>
        /// <param name="ids"></param>
        /// <param name="currentUser"></param>
        /// <returns></returns>
        public async ValueTask<ResponseContext<bool>> CancelPublish(List<long> ids, UserTicket currentUser)
        {
            var result = new ResponseContext<bool>();
            if (ids == null || !ids.Any())
            {
                return result;
            }

            var questions = await Question.FindAllAsync(Question._.Id.In(ids) & Question._.Status == QEnum.Started);
            if (questions.Any())
            {
                questions.ForEach(item =>
                {
                    item.Status = (int)QEnum.NotStart;
                    item.PublishTime = default(DateTime);
                    item.PublishBy = 0;
                });

                var i = questions.Save();
                result.SetSuccess();
            }
            return await Task.FromResult(result);
        }
    }
}